package cn.uyunsky.platform.util.other;

import java.security.MessageDigest;

public class MD5Tool {

	private static final String MD5 = "MD5";
	private static final String encode = "UTF-8";

	public static String encode(String value) {
		byte[] bs;
		try {
			bs = MessageDigest.getInstance(MD5).digest(value.getBytes(encode));
			return byte2hex(bs);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private static String byte2hex(byte[] b) // 二行制转字符串
	{
		StringBuffer hs = new StringBuffer();
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs.append("0").append(stmp);
			else
				hs = hs.append(stmp);

		}
		return hs.toString().toUpperCase();
	}

	public static void main(String[] args) {
		System.out.println(MD5Tool.encode("222222"));
	}

}
